<?php
    require("./Utils.php");

    $post = getJsonParamter();

    $username = $post['username'];

    $sql = "SELECT c.id id, image, name pet_name, price, number, stock
            FROM cart c LEFT JOIN pet p
            ON c.pet_id = p.id 
            LEFT JOIN user u
            ON c.user_id = u.username
            WHERE user_id = ?";

    $conn = getConnection();
    $pet_list = query_list($conn, $sql, $username);

    $total_price = 0;
    $data = array();

    foreach($pet_list as $pet){
        $total_price += $pet['price'] * $pet['number'];
    }

    $data['pet_list'] = $pet_list;
    $data['totalPrice'] = $total_price;

    getJson(200, '获取成功' , $data);
?>